/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2013 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "lw_smarty.h"

/* If you declare any globals in php_myfunctions.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(myfunctions)
*/

/* True global resources - no need for thread safety here */
zend_class_entry *lw_smarty_ce;

PHP_METHOD(lw_smarty, display){
}
PHP_METHOD(lw_smarty, fetch){
	
}
PHP_METHOD(lw_smarty, assign){
	uint argc = ZEND_NUM_ARGS();
	zval *key,*value;
	zval *tpl_vars;
	MAKE_STD_ZVAL(tpl_vars);
	array_init(tpl_vars);

	zval *old_tpl_vars = zend_read_property(lw_smarty_ce, getThis(), ZEND_STRL("_tpl_vars"), 1 TSRMLS_CC);

	//php_printf("not null\n");
	php_var_dump(&old_tpl_vars, 1 TSRMLS_CC);
	//php_printf("not null over\n");
	
	if(zend_parse_parameters(argc TSRMLS_CC, "z|z", &key, &value) == FAILURE) {
		return;
	}
	if((Z_TYPE_P(key) == IS_ARRAY) && (value != NULL)){
		php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "%s", "Param error!");
		return;
	}
	if(Z_TYPE_P(key) == IS_STRING){
		Z_ADDREF_P(value);
		add_assoc_zval(tpl_vars, Z_STRVAL_P(key), value);
	}else if(Z_TYPE_P(key) == IS_ARRAY){
		tpl_vars = key;
	}else{
		php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "%s", "first argument type error");
		return;
	}
	if(Z_TYPE_P(old_tpl_vars) != IS_NULL){
		zend_hash_merge(Z_ARRVAL_P(tpl_vars), Z_ARRVAL_P(old_tpl_vars), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *), 1);
	}
	zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("_tpl_vars"), tpl_vars TSRMLS_CC);
	return;
}

zend_function_entry lw_smarty_methods[] = {
	PHP_ME(lw_smarty, fetch, NULL, ZEND_ACC_PUBLIC)
	PHP_ME(lw_smarty, display, NULL, ZEND_ACC_PUBLIC)
	PHP_ME(lw_smarty, assign, NULL, ZEND_ACC_PUBLIC)
	{NULL, NULL ,NULL}
};

PHP_MINIT_FUNCTION(lw_smarty){
	zend_class_entry ce;
	INIT_CLASS_ENTRY(ce, "lw_Smarty", lw_smarty_methods);
	lw_smarty_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
	zend_declare_property_string(lw_smarty_ce, ZEND_STRL("template_dir"), "templates", ZEND_ACC_PROTECTED TSRMLS_CC);
	zend_declare_property_string(lw_smarty_ce, ZEND_STRL("compile_dir"), "templates_c", ZEND_ACC_PROTECTED TSRMLS_CC);
	zend_declare_property_string(lw_smarty_ce, ZEND_STRL("config_dir"), "configs", ZEND_ACC_PROTECTED TSRMLS_CC);
	zend_declare_property_bool(lw_smarty_ce, ZEND_STRL("debugging"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
	
	zend_declare_property_string(lw_smarty_ce, ZEND_STRL("left_delimiter"), "{", ZEND_ACC_PROTECTED TSRMLS_CC);
	zend_declare_property_string(lw_smarty_ce, ZEND_STRL("right_delimiter"), "}", ZEND_ACC_PROTECTED TSRMLS_CC);
	
	zend_declare_property_null(lw_smarty_ce, ZEND_STRL("_tpl_vars"), ZEND_ACC_PROTECTED TSRMLS_CC);
	
	//zval *tpl_vars;
	//MAKE_STD_ZVAL(tpl_vars);
	//array_init(tpl_vars);
	//zend_update_property(ce, getThis(), "age", sizeof("age")-1, age TSRMLS_CC);
	return SUCCESS;
}


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
